# GNAFU - "Ground, Navy and Air Fuck-Up" real-time strategy game
# Copyright (C) 2008-2009 k0wax <k0waxk0wax@gmail.com>
#
# This file is part of GNAFU.
#
# GNAFU is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# GNAFU is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GNAFU.  If not, see <http://www.gnu.org/licenses/>.

"""MVC Views module"""
from __future__ import division, absolute_import
import weakref

class EmptyView(object):
    pass

class BaseView(object):
    @classmethod
    def load_gfx(cls, self):
        pass
    @classmethod
    def load_sfx(cls, self):
        pass

def has_init_view(cls):
    """decorator for classes with init_view method"""
    def _wrap_init_view(meth):
        def wrapper(self, *args, **kw):
            self._view_dict = {} #weakref.WeakValueDictionary()
            predict = self.__dict__.copy()
            print '==> predict: ', predict
            retval = meth(self, *args, **kw)
            for k, v in self.__dict__.items():
                if k not in predict:
                    self._view_dict[k] = v
            print
            print '==> view_dict', self._view_dict
            return retval
        return wrapper
    
    cls.init_view = _wrap_init_view(cls.init_view)
    return cls

def add_init_view(cls):
    def wrapper(meth, *args, **kw):
        cls.init_view = meth
        return cls
    return wrapper

'''
class addview(object):
    def __init__(self, z):
        for name, meth in z.items():
            print 'add %s meth to %s class' % (meth, self)
            setattr(self, name, meth)
    def __call__(self, f):
        print self, f
        def wrapper(f, *args):
            print f, args
            return f(*args)
        return wrapper
'''

def lol(self):
    print 'LOL!'

_views = []

def addview(*args):
    def wrapper(cls):
        z = args[0]
        for name, meth in z.items():
            print 'add %s meth to %s class' % (cls, meth)
            setattr(cls, name, meth)
            #setattr(cls, name, lol)
            _views.append((cls, name, meth))
        return cls
    return wrapper

def create_views():
    for x in _views:
        print x
        setattr(*x)
        cls = x[0]
        #if hasattr(cls, 'init_view'):
        #    cls.init_view()

